/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.cms.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.cms.entity.CmsArticle;
import com.jeesite.modules.cms.service.CmsArticleService;

/**
 * 文章表Controller
 * @author 宋照照
 * @version 2020-12-04
 */
@Controller
@RequestMapping(value = "${adminPath}/cms/cmsArticle")
public class CmsArticleController extends BaseController {

	@Autowired
	private CmsArticleService cmsArticleService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public CmsArticle get(String id, boolean isNewRecord) {
		return cmsArticleService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("cms:cmsArticle:view")
	@RequestMapping(value = {"list", ""})
	public String list(CmsArticle cmsArticle, Model model) {
		model.addAttribute("cmsArticle", cmsArticle);
		return "modules/cms/cmsArticleList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("cms:cmsArticle:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<CmsArticle> listData(CmsArticle cmsArticle, HttpServletRequest request, HttpServletResponse response) {
		Page<CmsArticle> page = cmsArticleService.findPage(new Page<CmsArticle>(request, response), cmsArticle); 
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("cms:cmsArticle:view")
	@RequestMapping(value = "form")
	public String form(CmsArticle cmsArticle, Model model) {
		model.addAttribute("cmsArticle", cmsArticle);
		return "modules/cms/cmsArticleForm";
	}

	/**
	 * 保存文章表
	 */
	@RequiresPermissions("cms:cmsArticle:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated CmsArticle cmsArticle) {
		cmsArticleService.save(cmsArticle);
		return renderResult(Global.TRUE, text("保存文章表成功！"));
	}
	
	/**
	 * 删除文章表
	 */
	@RequiresPermissions("cms:cmsArticle:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(CmsArticle cmsArticle) {
		cmsArticleService.delete(cmsArticle);
		return renderResult(Global.TRUE, text("删除文章表成功！"));
	}
	
}